package edu.gdkm.web.controller;


import edu.gdkm.service.iface.AdminService;
import edu.gdkm.sql.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.ws.Action;
import java.util.List;

@Controller
@RequestMapping("/admin")
@CrossOrigin
@ResponseBody
public class AdminController {
    @Autowired
    private AdminService adminService;
    @RequestMapping("/login")
    public Admin login(@RequestBody Admin admin) {

        System.out.println(admin);
        Admin adminsql = adminService.findByUsername(admin.getUsername());

        if (adminsql != null) {
            if (adminsql.getPassword().equals(DigestUtils.md5DigestAsHex((DigestUtils.md5DigestAsHex(admin.getPassword().getBytes())+adminsql.getSalt()).getBytes()))) {
                return adminsql;
            }else {
                return null;
            }
        }
        return null;
    }
}
